package com.nekocode.gallery;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.nekocode.utils.MyAsyncTask;

import java.io.File;
import java.util.List;

/**
 * User: qii
 * Date: 14-5-1
 */
public class LongClickListener implements View.OnLongClickListener {

    private Activity context;

    private String url;

    private String filePath;

    private PicSaveTask saveTask;


    public LongClickListener(Activity activity, String url, String path) {
        this.context = activity;
        this.url = url;
        this.filePath = path;
    }

    private String getString(int res) {
        return context.getString(res);
    }

    public static boolean isIntentSafe(Activity activity, Intent intent) {
        PackageManager packageManager = activity.getPackageManager();
        List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
        return activities.size() > 0;
    }

    @Override
    public boolean onLongClick(View v) {
        String[] values = {getString(R.string.save_pic_album),
                getString(R.string.share)};

        new AlertDialog.Builder(context)
                .setItems(values, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                            	saveBitmapToPictureDir(filePath);
                                break;
                            case 1:
                                Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                                sharingIntent.setType("image/jpeg");
                                if (!TextUtils.isEmpty(filePath)) {
                                    Uri uri = Uri.fromFile(new File(filePath));
                                    sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
                                    if (isIntentSafe(context,
                                            sharingIntent)) {
                                        context.startActivity(Intent.createChooser(sharingIntent,
                                                getString(R.string.share)));
                                    }
                                }
                                break;
                        }
                    }
                }).show();

        return true;
    }

    private boolean isTaskStopped(MyAsyncTask task) {
        return task == null || task.getStatus() == MyAsyncTask.Status.FINISHED;
    }

    private void saveBitmapToPictureDir(String filePath) {
        if (isTaskStopped(saveTask)) {
            saveTask = new PicSaveTask(context, filePath);
            saveTask.executeOnNormal();
        }

    }
}

